<template>
  <div class="product-detail">
    <el-form
      ref="productFormRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      :disabled="type === 'view'"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="商品ID" prop="id">
            <el-input v-model="formData.id" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商品名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入商品名称" />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="商品分类" prop="category">
            <el-select v-model="formData.category" placeholder="请选择分类">
              <el-option label="电子产品" value="电子产品" />
              <el-option label="服装" value="服装" />
              <el-option label="食品" value="食品" />
              <el-option label="图书" value="图书" />
              <el-option label="家居" value="家居" />
              <el-option label="运动" value="运动" />
              <el-option label="美妆" value="美妆" />
              <el-option label="汽车" value="汽车" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商品价格" prop="price">
            <el-input-number
              v-model="formData.price"
              :min="0"
              :precision="2"
              placeholder="请输入价格"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="库存数量" prop="stock">
            <el-input-number
              v-model="formData.stock"
              :min="0"
              placeholder="请输入库存数量"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="商品状态" prop="status">
            <el-radio-group v-model="formData.status">
              <el-radio :label="1">上架</el-radio>
              <el-radio :label="0">下架</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      
      <el-form-item label="商品描述" prop="description">
        <el-input
          v-model="formData.description"
          type="textarea"
          :rows="4"
          placeholder="请输入商品描述"
        />
      </el-form-item>
      
      <el-form-item label="创建时间">
        <el-input v-model="formData.createTime" disabled />
      </el-form-item>
      
      <el-form-item label="商品图片" prop="images">
        <el-upload
          class="upload-demo"
          action="#"
          :auto-upload="false"
          :file-list="fileList"
          list-type="picture"
          :disabled="type === 'view'"
        >
          <el-button type="primary" v-if="type === 'edit'">选择图片</el-button>
        </el-upload>
      </el-form-item>
      
      <el-form-item label="库存状态">
        <el-tag :type="getStockStatusType(formData.stock)">
          {{ getStockStatusText(formData.stock) }}
        </el-tag>
      </el-form-item>
    </el-form>
    
    <div class="form-actions" v-if="type === 'edit'">
      <el-button type="primary" @click="handleSave">保存</el-button>
      <el-button @click="handleReset">重置</el-button>
      <el-button @click="handleClose">关闭</el-button>
    </div>
    
    <div class="form-actions" v-else>
      <el-button type="primary" @click="handleEdit">编辑</el-button>
      <el-button @click="handleClose">关闭</el-button>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus'

export default {
  name: 'ProductDetail',
  props: {
    data: {
      type: Object,
      required: true
    },
    type: {
      type: String,
      default: 'view' // view, edit
    }
  },
  data() {
    return {
      formData: {
        id: '',
        name: '',
        category: '',
        price: 0,
        stock: 0,
        description: '',
        status: 1,
        createTime: ''
      },
      fileList: [],
      formRules: {
        name: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        category: [
          { required: true, message: '请选择商品分类', trigger: 'change' }
        ],
        price: [
          { required: true, message: '请输入商品价格', trigger: 'blur' },
          { type: 'number', min: 0, message: '价格必须大于等于0', trigger: 'blur' }
        ],
        stock: [
          { required: true, message: '请输入库存数量', trigger: 'blur' },
          { type: 'number', min: 0, message: '库存数量必须大于等于0', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.initFormData()
  },
  methods: {
    initFormData() {
      this.formData = { ...this.data }
      if (!this.formData.description) {
        this.formData.description = ''
      }
    },
    
    getStockStatusType(stock) {
      if (stock === 0) return 'danger'
      if (stock < 10) return 'warning'
      return 'success'
    },
    
    getStockStatusText(stock) {
      if (stock === 0) return '缺货'
      if (stock < 10) return '库存不足'
      return '库存充足'
    },
    
    handleSave() {
      this.$refs.productFormRef.validate((valid) => {
        if (valid) {
          this.$emit('save', this.formData)
          ElMessage.success('保存成功')
        }
      })
    },
    
    handleReset() {
      this.initFormData()
      ElMessage.info('已重置表单')
    },
    
    handleEdit() {
      // 切换到编辑模式
      const currentRoute = this.$route
      this.$router.push({
        path: currentRoute.path,
        query: {
          ...currentRoute.query,
          action: 'edit'
        }
      })
    },
    
    handleClose() {
      this.$emit('close', this.$route.query.type + '-' + this.$route.query.id + '-' + this.$route.query.action)
    }
  },
  
  watch: {
    data: {
      handler(newData) {
        this.initFormData()
      },
      deep: true
    }
  }
}
</script>

<style scoped>
.product-detail {
  max-width: 800px;
}

.form-actions {
  margin-top: 30px;
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid #e4e7ed;
}

.form-actions .el-button {
  margin: 0 10px;
}

.el-form-item {
  margin-bottom: 20px;
}

.upload-demo {
  width: 100%;
}
</style>